<template>
  <div>
    <el-container>
      <el-header>
        <h3>Header</h3>
      </el-header>
      <el-container>
        <el-aside class="home_aside" width="170px">
          <div class="home_aside_data">
            <p>PRICE:</p>
            <p
              @click="onClick(item,index)"
              :class="selIndex==index?'home_aside_data_p_active':'home_aside_data_p'"
              v-for="(item,index) in slider"
              :key="index"
            >{{item.title}}</p>
          </div>
        </el-aside>
        <el-main>
          <div class="main_search">
            <p class="home_sort" @click="onSorts(sort)">SORT</p>
            <p class="iconfont icon-gouwuche" @click="goCart"></p>
          </div>

          <div class="home_container">
            <el-row :gutter="10">
              <el-col
                class="col"
                v-for="(item,index) in showlist"
                :key="index"
                :xs="24"
                :sm="8"
                :md="6"
                :lg="4"
                :xl="2"
              >
                <div class="grid-content bg-purple">
                  <img width="100%" :src="item.productImage" alt />
                  <div class="home_card">
                    <p class="home_card_name">{{item.productName}}</p>
                    <p class="home_card_price">￥{{item.salePrice}}.00</p>
                    <p class="home_card_add" @click="addcart(item)">加入购物车</p>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-main>
      </el-container>
    </el-container>

    <!-- 上拉加载 -->
    <div class="loading_container" v-isLoading="isloading">
      <img width="20%" src="https://img.zcool.cn/community/01aa2a5715ffc932f8758c9b594176.gif" alt />
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      slider: [],
      showlist: [],
      selIndex: 0,
      sort: false,
      isloading: false,
      page: 1
    };
  },
  mounted() {
    this.$axios.get("http://localhost:8080/data.json").then(res => {
      this.slider = res.data.result.slider;
      this.list = res.data.result.list;
      this.showlist = res.data.result.list;

      //监听文档的高度
      window.addEventListener("scroll", () => {
        //获取文档的高度
        var documentHeight = document.documentElement.scrollHeight;

        //获取当前窗口的高度(可视区域)
        var client = document.documentElement.clientHeight;
        console.log(client);

        //获取滚动距离
        var scrollOffset = document.documentElement.scrollTop;
        console.log(scrollOffset)
        if (documentHeight - client - scrollOffset === 0) {
          window.console.log("111111");
          this.isloading = true;
          //执行异步请求
          window.setTimeout(() => {
            if (this.page > 3) {
              return;
            }
            this.$axios
              .get(`http://localhost:8080/data${++this.page}.json`)
              .then(res => {
                this.showlist = [...this.showlist, ...res.data.result.list];
              });
            this.isloading = false;
          }, 3000);
        }
      });
    });
  },
  methods: {
    onClick(item, index) {
      this.selIndex = index;
      this.showlist = [];
      if (item.title == "All") {
        this.showlist = this.list;
      } else {
        this.list.forEach(ele => {
          if (ele.salePrice >= item.low && ele.salePrice <= item.hight) {
            this.showlist.push(ele);
          }
        });
      }
    },
    onSorts(sort) {
      this.sort = !this.sort; //排序
      if (sort) {
        this.showlist.sort((a, b) => {
          return a.salePrice - b.salePrice;
        });
      } else {
        this.showlist.sort((a, b) => {
          return b.salePrice - a.salePrice;
        });
      }
    },
    //点击加入购物车
    addcart(item) {
      this.$store.dispatch('AddCart',item)
    },
    //点击跳转到购物车页面
    goCart() {
      this.$router.push('cart');
    }
  },
  directives: {
    isLoading: {
      bind() {},
      inserted(el, binding) {
        if (binding.value) {
          el.style.visibility = "visible";
        } else {
          el.style.visibility = "hidden";
        }
      },
      update(el, binding) {
        if (binding.value) {
          el.style.visibility = "visible";
        } else {
          el.style.visibility = "hidden";
        }
      }
    }
  }
};
</script>

<style scoped>
.main_search {
  width: 100%;
  height: 40px;
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 2px 10px;
}
.home_sort {
  width: 80px;
  height: 40px;
  border-radius: 30px;
  cursor: pointer;
  text-align: center;
  line-height: 40px;
  background-color: #d3dce6;
}
.icon-gouwuche {
  font-size: 32px;
  margin-right: 150px;
}
.el-col {
  border-radius: 4px;
}
.el-row {
  width: 100%;
  display: flex;
  justify-content: start;
  flex-wrap: wrap;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.el-header {
  background-color: #b3c0d1;
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  box-sizing: border-box;
  padding: 10px;
}

.el-main {
  background-color: #efefef;
  color: #333;
  font-size: 16px;
}
.home_container {
  width: 100%;
  display: flex;
  justify-content: start;
}
.col {
  margin: 10px;
  background-color: #fff;
}
.col:hover {
  border: 2px solid red;
  transition: all 0.2s ease-in-out;
  transform: translateY(-5px);
}
@media screen and (max-width: 768px) {
  .home_aside {
    display: none;
  }
  .home_container {
    width: 100%;
  }
  .grid-content {
    width: 100%;
    display: flex;
    justify-content: space-between;
    position: relative;
  }
  .grid-content img {
    width: 90px;
    border: 1px solid lightgrey;
    margin: 5px;
  }
  .col {
    margin: 5px 0px !important;
  }
  .home_card {
    width: 83%;
    /* border: 1px solid red; */
    box-sizing: border-box;
    padding: 0px 10px;
  }
  .home_card_name {
    margin-top: 10px;
  }
  .home_card_add {
    width: 100px;
    position: absolute;
    top: 65px;
    right: 10px;
  }
  .home_card_price {
    margin-bottom: 40px !important;
  }
  .icon-gouwuche {
    margin-right: 20px;
  }
}
.home_aside_data {
  line-height: 40px;
}
.home_aside_data_p_active {
  border-left: 3px solid red;
  text-indent: 5px;
  padding: 0px 5px;
}
.home_aside_data_p {
  text-indent: 5px;
  /* border: 1px solid #000; */
  padding: 0px 5px;
}
.home_card_add {
  color: red;
  line-height: 35px;
  text-align: center;
  margin-top: 10px;
  margin-bottom: 10px;
  border: 1px solid red;
  cursor: pointer;
}
.home_card_price {
  color: red;
  margin-top: 15px;
  margin-bottom: 10px;
  font-size: 22px;
}
.loading_container {
  width: 100%;
  text-align: center;
}
</style>